import { Injectable } from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs';
import {map} from 'rxjs/internal/operators';

@Injectable({
  providedIn: 'root'
})
export class MusicService {
  /*用户名*/
  public musicUsername = '18660683370';
  /*用后密码*/
  public musicPassword = '123ADD123ADD';
  /*音乐列表*/
  public listMusic = null;
  /*登录返回的 标志*/
  public login = null;
  constructor(private http: Http) { }

  /*音乐登录*/
  public loginMusic(): Observable<any> {
      return this.http.get('http://47.106.104.22:3000/login/cellphone?phone=' + this.musicUsername + '&password=' + this.musicPassword + '')
        .pipe(
          map(res => {
            console.log(res);
            res.json();
          } )
        );
  }
  /*获取音乐资源的列表*/
  public acquireMusicList(): Observable<any> {
    return this.http.get('http://47.106.104.22:3000/personalized/newsong').pipe(
      map(res => res.json())
    );
  }
  /*获取音乐资源的url*/
  public takeUrlMusic(id: number): Observable<any> {
    return this.http.get('https://api.imjad.cn/cloudmusic/?type=song&id=' + id + '').pipe(
      map(res => res.json())
    );
  }

  /**
   * 设置cookie时长
   * @param time
   * @returns {Date}
   */
  public setCookie(time: number): Date {
    const date = new Date();
    date.setMinutes(date.getMinutes() + time);
    return date;
  }
}

// 单个·歌曲对象
export class Song {
  constructor(
    private id: string,
    public name: string, // 歌曲名字
    public artistsName: string, // 歌手名字
    public blurPicUrl: string, // 歌曲封面图片
    public dt?: number, // 歌曲时长
  ) {}
}
